package cn.jietuo.first.class06;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/7/19
 * @description: 给定两个可能有环也可能无环的单链表，头节点head1和head2。请实现一个函数，如果两个链表相交，
 * 请返回相交的 第一个节点。如果不相交，返回null
 * 【要求】
 * 如果两个链表长度之和为N，时间复杂度请达到O(N)，额外空间复杂度 请达到O(1)。
 */
public class Code05_FindFirstIntersectNode {

    public static class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node loopNode1 = getLoopNode(head1);
        Node loopNode2 = getLoopNode(head2);
        if (loopNode1 == null && loopNode2 == null) {
            // 两个都没有环
            return noLoop(head1, head2);
        } else if (loopNode1 != null && loopNode2 != null) {
            // 两个都有环
            return bothLoop(head1, loopNode1, head2, loopNode2);
        } else {
            // 一个有环，一个没环，不存在
            return null;
        }

    }

    private static Node bothLoop(Node head1, Node loopNode1, Node head2, Node loopNode2) {
        // 有两种情况 一种入环点相同，一种入环点不同
        if (loopNode1 == loopNode2) {
            // 入环点相同 ,相当于无环求解
            Node cur1 = head1;
            Node cur2 = head2;
            int n = 0;
            while (cur1 != loopNode1) {
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loopNode2) {
                n--;
                cur2 = cur2.next;
            }
            cur1 = n >= 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);
            while (n != 0) {
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } else {
            // 入环点不相同 有两种情况，一种是不想交，一种是相交
            Node cu1 = loopNode1.next;
            while (cu1 != loopNode1) {
                if (cu1 == loopNode2) {
                    return loopNode1;
                }
                cu1 = cu1.next;
            }
            return null;
        }
    }

    private static Node noLoop(Node head1, Node head2) {
        // 先求出Node的长度，从短的node头开始遍历比较，因为如果相交，那么后面的结点肯定都相同
        Node cur1 = head1;
        Node cur2 = head2;
        int n = 0;
        while (cur1 != null) {
            n++;
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            n--;
            cur2 = cur2.next;
        }
        if (cur1 != cur2) {
            return null;
        }
        cur1 = n > 0 ? head1 : head2;
        cur2 = cur1 == head1 ? head2 : head1;
        n = Math.abs(n);
        while (n != 0) {
            n--;
            cur1 = cur1.next;
        }
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;

    }


    private static int getNodeLength(Node head1) {
        if (head1 == null) {
            return 0;
        }
        int length = 0;
        Node cur = head1;
        while (cur != null) {
            length++;
            cur = cur.next;

        }
        return length;
    }

    /**
     * 获取此链表第一次入环的结点，如果没有环，则返回null
     */
    public static Node getLoopNode(Node head) {
        // 想形成一个环，最好需要3个结点
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        Node slow = head.next;
        Node fast = head.next.next;
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        fast = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }


    public static void main(String[] args) {
// 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4
        System.out.println(getIntersectNode(head1, head2));

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);
    }
}
